<template>
  <HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过">
    <ul class="goods-list">
      <li v-for="item in goods" :key="item.id">
        <RouterLink to="/">
          <img :src="item.picture" alt="" />
          <p class="name">{{ item.title }}</p>
          <p class="desc">{{ item.alt }}</p>
        </RouterLink>
      </li>
    </ul>
    <template #right>
      <xtx-more></xtx-more>
    </template>
  </HomePanel>
</template>

<script>
import HomePanel from './home-pannel.vue'
import { findHot } from '@/api/home'
import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'HomeHot',
  components: { HomePanel },
  setup () {
    const goods = ref([])
    async function setGoods () {
      const data = await findHot()
      goods.value = data.result
    }

    // 判断组件进入视口
    const target = ref(null)

    const { stop } = useIntersectionObserver(
      target,
      ([{ isIntersecting }], observerElement) => {
        // targetIsVisible.value = isIntersecting
        // console.log('正在观察', isIntersecting)
        if (isIntersecting) {
          stop() // 进入视口则停止observe 并发送api请求
          setGoods()
        }
      }
    )
    return {
      goods,
      target
    }
  }
}

// vue2.0
// data () {
//   return {
//     goods: []
//   }
// },
// async created () {
//   this.goods = data.result
// }
// }
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
